package com.quanyan.place.web;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.quanyan.api.APIResponse;
import com.quanyan.place.entity.apireq.ReqBannerAdmin;
import com.quanyan.place.entity.apireq.ReqBannerByCondition;
import com.quanyan.place.entity.apireq.ReqBannerPosition;
import com.quanyan.place.service.AppBannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * Created by yue.yuan on 2017/5/24.
 */
@RestController
@RequestMapping(value = "/admin/api/place/banner", method = RequestMethod.POST)
public class AppBannerController {

    @Autowired
    private AppBannerService appBannerService;

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String addBanner(@RequestBody @Valid ReqBannerAdmin reqBannerAdmin,
                            BindingResult bindingResult){

        APIResponse response = appBannerService.createBannerInfo(reqBannerAdmin);
        return JSONObject.toJSONString(response,
                SerializerFeature.WriteNullBooleanAsFalse,
                SerializerFeature.WriteNullListAsEmpty,
                SerializerFeature.WriteNullNumberAsZero,
                SerializerFeature.WriteMapNullValue,
                SerializerFeature.WriteNullStringAsEmpty);
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String updateBanner(@RequestBody ReqBannerAdmin reqBannerAdmin,
                               BindingResult bindingResult){

        if(null == reqBannerAdmin.getId() || reqBannerAdmin.getId() == 0) {
            return JSONObject.toJSONString(APIResponse.returnFail("id can not be null"));
        }
        APIResponse response = appBannerService.updateBannerInfo(reqBannerAdmin);
        return JSONObject.toJSONString(response,
                SerializerFeature.WriteNullBooleanAsFalse,
                SerializerFeature.WriteNullListAsEmpty,
                SerializerFeature.WriteNullNumberAsZero,
                SerializerFeature.WriteMapNullValue,
                SerializerFeature.WriteNullStringAsEmpty);
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public String deleteBanner(@RequestBody ReqBannerAdmin reqBannerAdmin,
                               BindingResult bindingResult){
        if(null == reqBannerAdmin.getId() || reqBannerAdmin.getId() == 0) {
            return JSONObject.toJSONString(APIResponse.returnFail("id can not be null"));
        }
        APIResponse response =
                appBannerService.deleteBanner(reqBannerAdmin.getId());
        return JSONObject.toJSONString(response,
                SerializerFeature.WriteNullBooleanAsFalse,
                SerializerFeature.WriteNullListAsEmpty,
                SerializerFeature.WriteNullNumberAsZero,
                SerializerFeature.WriteMapNullValue,
                SerializerFeature.WriteNullStringAsEmpty);
    }

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public String listBanners(@RequestBody ReqBannerByCondition reqBannerByCondition,
                              BindingResult bindingResult){
        APIResponse response =
                appBannerService.listBannerInfo(reqBannerByCondition);
        return JSONObject.toJSONString(response,
                SerializerFeature.WriteNullBooleanAsFalse,
                SerializerFeature.WriteNullListAsEmpty,
                SerializerFeature.WriteNullNumberAsZero,
                SerializerFeature.WriteMapNullValue,
                SerializerFeature.WriteNullStringAsEmpty);
    }

    @RequestMapping(value = "/listLayouts", method = RequestMethod.POST)
    public String listLayouts(){

        APIResponse response = appBannerService.listBannerLayouts();
        return JSONObject.toJSONString(response,
                SerializerFeature.WriteNullBooleanAsFalse,
                SerializerFeature.WriteNullListAsEmpty,
                SerializerFeature.WriteNullNumberAsZero,
                SerializerFeature.WriteMapNullValue,
                SerializerFeature.WriteNullStringAsEmpty);
    }

    @RequestMapping(value = "/layout", method = RequestMethod.POST)
    public String layouts(@RequestBody ReqBannerPosition position,
                          BindingResult bindingResult){

        APIResponse response = appBannerService.queryLayout(position.getPositionId());
        return JSONObject.toJSONString(response,
                SerializerFeature.WriteNullBooleanAsFalse,
                SerializerFeature.WriteNullListAsEmpty,
                SerializerFeature.WriteNullNumberAsZero,
                SerializerFeature.WriteMapNullValue,
                SerializerFeature.WriteNullStringAsEmpty);
    }

    @RequestMapping(value = "/updateLayouts", method = RequestMethod.POST)
    public String deletePosition(@RequestBody @Valid ReqBannerPosition reqBannerPosition,
                                 BindingResult bindingResult){

        APIResponse response =
                appBannerService.updatePosition(reqBannerPosition);
        return JSONObject.toJSONString(response,
                SerializerFeature.WriteNullBooleanAsFalse,
                SerializerFeature.WriteNullListAsEmpty,
                SerializerFeature.WriteNullNumberAsZero,
                SerializerFeature.WriteMapNullValue,
                SerializerFeature.WriteNullStringAsEmpty);
    }
}
